package org.noworks.game;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.noworks.game.engine.Sprite;

public class MainMenu extends BasicGameState {

	int stateID = -1;

	private Sprite background;
	private Sprite optionsImg;
	private Sprite selectChapterImg;
	private Sprite startImg;
	private Sprite exitImg;

	public MainMenu(int stateID) {
		this.stateID = stateID;
	}

	@Override
	public void init(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		// TODO Auto-generated method stub
		background = new Sprite(new Image(
				"data/img/backgrounds/mainMenu/mainMenu-1920x1080.png"), 0, 0,
				Main.scale);
		optionsImg = new Sprite(new Image(
				"data/img/backgrounds/mainMenu/options.png"), 343, 700,
				Main.scale);
		selectChapterImg = new Sprite(new Image(
				"data/img/backgrounds/mainMenu/selectChapter.png"), 206, 523,
				Main.scale);
		startImg = new Sprite(new Image(
				"data/img/backgrounds/mainMenu/startGame.png"), 265, 342,
				Main.scale);
		exitImg = new Sprite(
				new Image("data/img/backgrounds/mainMenu/exit.png"), 407, 849,
				Main.scale);
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
			throws SlickException {
		// TODO Auto-generated method stub
		background.draw();
		optionsImg.draw();
		selectChapterImg.draw();
		startImg.draw();
		exitImg.draw();
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta)
			throws SlickException {
		// TODO Auto-generated method stub
		Input in = gc.getInput();
		int mx = in.getMouseX();
		int my = in.getMouseY();
		
		boolean inStart = false, inExit = false, inOptions = false, inChapter = false;
		
		if (optionsImg.isPointInside(mx, my))
			inOptions = true;
		else if (startImg.isPointInside(mx, my))
			inStart = true;
		else if (exitImg.isPointInside(mx, my))
			inExit = true;
		else if (selectChapterImg.isPointInside(mx, my))
			inChapter = true;
		
		if (in.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
			if (inStart)
				sbg.enterState(Main.GAMEPLAY_STATE);
			else if (inExit)
				gc.exit();
			else if (inOptions)
				sbg.enterState(Main.OPTIONS_STATE);
			else if (inChapter)
				sbg.enterState(Main.CHAPTER_STATE);
		}

	}

	@Override
	public int getID() {
		return stateID;
	}

}
